package contegor.scire.core.queries;

import contegor.scire.core.Concept;
import contegor.scire.core.Container;
import contegor.scire.core.Id;
import contegor.scire.core.IdFragment;
import contegor.scire.core.TaskId;
import contegor.scire.core.Version;

/**
 * {@link Query} that returns {@link Concept}s with the {@link Id}s given to the constructor. It makes
 * {@link Container}s expansion: if the {@link Id} is from a {@link Container}, then it returns its children.
 *
 * @version $Revision$
 * @author  Héctor Cirbián Pousa ($Author$)
 */
@Version("$Id$")
public class GetConceptsExpanded extends Query<Concept> {
  private static final long serialVersionUID = -423582669472666504L;

  private IdFragment[] ids;

  /** <b>Internal use only. </b>*/
  protected GetConceptsExpanded(TaskId taskId, Id targetContainerId, IdFragment ... ids) {
    super(taskId, targetContainerId);
    this.ids = ids;
  }

  @Override
  public void process(Container targetContainer) {
    for (IdFragment id: ids) {
      Concept c = targetContainer.getConcept(id);
      if ((c != null) && c.isVisibleTo(this.taskId.getTransactionId())) {
        if (c instanceof Container)
          this.returnPoint.manage(this.taskId, ((Container)c).getConcepts());
        else
          this.returnPoint.manage(this.taskId, c);
      }
    }
  }

}
